package com.example.cpl.gjc;



import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class teach_center extends AppCompatActivity {
    private Button login;
    private TextView teac_account,teac_password;
    private int t=0;
    public void initWidght(){
        login=(Button)findViewById(R.id.login);

        teac_account=(TextView)findViewById(R.id.teac_account);
        teac_password=(TextView)findViewById(R.id.teac_password);

    }
    String filename1 = "teacher";
    static String who=null;

    public String read_SP_Str(String s,String s1) {     //读取数据库的 字符串类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        return  someData.getString(""+s+"",""+s1+"");
    }
    public int read_SP_Int(String s,int s1) {       //读取数据库的 数值类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        return  someData.getInt(""+s+"",s1);
    }


    public void  write_SP_Str(String s,String s1) { //写入数据库的 字符串类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putString(""+s+"",""+s1+"");
        editor.commit();// 提交修改
    }
    public void  write_SP_Int(String s,int s1) {            //写入数据库的 数值类型的值
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putInt(""+s+"",s1);
        editor.commit();// 提交修改
    }
    public void clear_SP()
    {
        final SharedPreferences someData = getSharedPreferences(filename1, 0);
        SharedPreferences.Editor editor = someData.edit();
        editor.clear();
        editor.commit();

    }
    private void setSP() {

        t= read_SP_Int("value", 0);
        Toast.makeText(getApplicationContext(), "注册数："+t, Toast.LENGTH_SHORT)
                .show();//阴影提示框
        // Log.d("存储数量：",a);
//        for (int i =0; i < t ; i++) {              //循环写入历史数据
//            String account = read_SP_Str("体重" + "" + i + "", "");
//            String password = read_SP_Str("身高" + "" + i + "", "");
//
//
//
//       //     tv_data.setText(tv_data.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "    " + tz_data +"\n"+bmi_data+"\n\n");
//        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teach_center);
        initWidght();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setSP();//循环写入历史数据
        login.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("PrivateResource")
            @Override
            public void onClick(View view) {
                if (teac_account.getText().toString().equals("8888")) {
                    who="8888";          //赋值登录人员
                    Intent intent = new Intent(teach_center.this, MainActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
                    return;
                }
                for(int p=0;p<t;p++) {
                    String account=null;                     //得到记录的数据
                    account=read_SP_Str("account" + "" + p + "","");
                    String password=null;
                    password=read_SP_Str("password" + "" + p + "","");


                    if (teac_account.getText().toString().equals(account) && teac_password.getText().toString().equals(password)) {
                        who=teac_account.getText().toString();          //赋值登录人员
                        Intent intent = new Intent(teach_center.this, MainActivity.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
                        return;
                    }
                }

                Toast.makeText(getApplicationContext(), "账号/密码错误！", Toast.LENGTH_SHORT)
                        .show();//阴影提示框


            }
        });

//        teac_sign.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
//
//                String account=null;                     //得到记录的数据
//                account=teac_account.getText().toString();
//                String password=null;
//                password=teac_password.getText().toString();
//
//                if(!account.equals("")&&!password.equals("")){
//
//                    write_SP_Str("account" + "" + t + "",  account);
//                    write_SP_Str("password" + "" + t + "",  password);
//
//                    t++;
//                    write_SP_Int("value", t);
//                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT)
//                            .show();//阴影提示框
//
//                }else{
//                    Toast.makeText(getApplicationContext(), "输入错误", Toast.LENGTH_SHORT)
//                            .show();//阴影提示框
//                }
//
//
//            }
//        });
//
//
//        teac_logout.setOnClickListener(new View.OnClickListener() {
//            @SuppressLint("PrivateResource")
//            @Override
//            public void onClick(View view) {
//
//                teac_account.setText("");
//                teac_password.setText("");
//
//                Toast.makeText(getApplicationContext(), "已注销账号！", Toast.LENGTH_SHORT)
//                        .show();//阴影提示框
//
//            }
//        });

    }
}
